﻿package com.t3.fx  
{	
	import com.t3.tuio.TUIOEvent;
	import com.t3.tuio.TUIOObject;
	import flash.utils.Dictionary;
	
	import flash.display.Sprite;
	 
	public class CharArea extends TUIOObject
	{
		private var _background:Sprite = new Sprite();
		private var _lettersprite:Sprite = new Sprite();
		private var rest:uint = 10; 
		private var _letters:Array = new Array();
		private var activeChars:uint = 0;
		
		public static const ID:uint = 4; 
		
		public function CharArea( event:TUIOEvent ) : void
		{
			super( event, true );
			
			_background.graphics.lineStyle(5, 0, 1);
			_background.graphics.beginFill(0xFFFFFF, 0.95)
			_background.graphics.drawCircle(-5, 5, 215);
			_background.graphics.endFill();
			_background.graphics.lineStyle();
			_background.graphics.beginFill(0xCCCCCC, 1)
			_background.graphics.drawCircle(-5, 5, 90);
			_background.graphics.endFill();
			this.addChild(_background);
			this.addChild(_lettersprite);
			
			initLetters();
			
			update( event, true );
		}
		
		protected override function repaint():void {}
		
		private function initLetters():void
		{
			var str_letters:String = "abcdefghijklmnopqrstuvwxyz";
			
			for ( var t_i:uint = 0; t_i < str_letters.length-rest; t_i++ )
			{
				var letter:Letter = new Letter( str_letters.charAt(t_i), "letter" );
						
				_letters.push(letter);
				_lettersprite.addChild(letter);
			}
			
			for ( var t_i2:uint = str_letters.length -rest ; t_i2 < str_letters.length; t_i2++ )
			{
				var letter2:Letter = new Letter( str_letters.charAt(t_i2), "letter" );
						
				_letters.push(letter2); 
				
				_lettersprite.addChild(letter2);
			}
		}
		
		public override function update( event:TUIOEvent, ignoreEvent:Boolean = false ):void
		{	
			if ( activeChars == 0 )
			{
				if( ignoreEvent || event.f_id == this.f_id && ( event.x != _x || event.y != _y || event.a != _a ) )
				{
					super.update( event );			
					
					for ( var t_i:uint = 0; t_i < _letters.length-rest; t_i++ )
					{
						var letter:Letter = _letters[t_i];
						letter.x = Math.sin(t_i / (_letters.length - rest) * Math.PI * 2 + (a/Math.PI*Math.PI)) * 180-25;
						letter.y = Math.cos(t_i / (_letters.length - rest) * Math.PI * 2 + (a/Math.PI*Math.PI)) * 180-25;
					}
					
					for ( var t_i2:uint = _letters.length -rest ; t_i2 < _letters.length; t_i2++ )
					{
						var letter2:Letter = _letters[t_i2];
						letter2.x = Math.sin(t_i2 / rest * Math.PI * 2 - (a/Math.PI*Math.PI)) * 130-25;
						letter2.y = Math.cos(t_i2 / rest * Math.PI * 2 - (a/Math.PI*Math.PI)) * 130-25;
					}
				}
			}
		}
		
		public function get letters():Array
		{
			return _letters;
		}
		
		public function addChar() : void 
		{		
			activeChars++;
			trace("adding activechars" + activeChars);
		}
		
		public function removeChar( ) : void
		{
			activeChars--;
			trace("removing activechars new activechars is: " +activeChars);
		}
		
		public function get background():Sprite 
		{
			return _background;
		}
	}
}